#!/usr/bin/python

"""Google Drive."""

__author__ = 'junda'

import httplib2
import sys

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run

DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive.file'

def build_service(name, verstr, scope, secret='client_secrets.json'):
  cred_fn = '%s_%s.cred' % (name, verstr)
  storage = Storage(cred_fn)
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = run(flow_from_clientsecrets(secret, scope), storage)
  http = httplib2.Http()
  http = credentials.authorize(http)

  service = build(name, verstr, http=http)
  return service

def main(argv):
  service = build_service('drive', 'v2', DRIVE_SCOPE)

  fname = argv[1]
  media = MediaFileUpload(fname, resumable=True)
  file = service.files().insert(
      media_body=media,
      body={'title': fname}).execute()
  print file['id']

if __name__ == '__main__':
  main(sys.argv)

